<template>
  <div class="px-4 h-full overflow-hidden">
    <div v-if="!isShowTemperature">
      <div class="flex justify-between items-center py-2">
        <a-form class="flex flex-wrap">
          <a-form-item>
            <a-input-search v-model:value="searchKey" class="w-64" placeholder="姓名/助记码/住院号" @search="getData" @keyup.enter="getData" />
          </a-form-item>
          <a-form-item label="出院日期" class="ml-4">
            <a-range-picker v-model:value="dateRange" class="w-64" format="YYYY-MM-DD" value-format="YYYY-MM-DD" @change="getData" />
          </a-form-item>
        </a-form>
        <!-- <a-button type="primary" @click="open = true">导出</a-button> -->
        <BaseExport
          type="住院"
          name="已出院"
          code="000181"
          :search-params="{
            stdate: dateRange[0],
            eddate: dateRange[1],
            status: store.tabsKey,
            bqbh: store.areaCode,
            pyjm: searchKey
          }"
          >导出</BaseExport
        >
      </div>
      <BaseTable column-code="000181" :loading="loading" :scroll="{ x: 2600 }" :data-source="tableData" :pagination="pagination" fill>
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'action'">
            <a-button type="link" size="small" @click="onPrintAdv(record, '1')">长嘱单</a-button>
            <a-button type="link" size="small" @click="onPrintAdv(record, '0')">临嘱单</a-button>
            <a-button type="link" size="small" @click="onPrintTemp(record)">体温脉搏单</a-button>
            <TableMoreButton>
              <a-button type="link" size="small" @click="onPrintNursingRecord(record, '1')">护理记录单</a-button>
              <a-button type="link" size="small" @click="onPrintBlood(record)">血糖单</a-button>
            </TableMoreButton>
          </template>
        </template>
      </BaseTable>
    </div>
    <div v-else class="h-full">
      <TabTemperature :key="rowData.inplsh" :inplsh="rowData.inplsh" is-show-back @back="isShowTemperature = false" />
    </div>
  </div>
</template>
<script setup>
import { ref } from 'vue'
import dayjs from 'dayjs'
import { message } from 'ant-design-vue'
import BaseTable from '@/components/BaseTable'
import TabTemperature from '@/views/in-doctor/workstation/components/TabTemperature'
import useStore from '../../store'

const store = useStore()
const searchKey = ref('')
const dateRange = ref([dayjs().subtract(7, 'day').format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')])
const isShowTemperature = ref(false)
const rowData = ref({})
const { loading, tableData, pagination, getData } = useTable({
  url: '/inp/zydj/getInpRecordList',
  immediate: true,
  // pagination: false,
  formatParams: () => {
    return {
      stdate: dateRange.value[0],
      eddate: dateRange.value[1],
      status: store.tabsKey,
      bqbh: store.areaCode,
      pyjm: searchKey.value
    }
  }
})

const onPrintTemp = (record) => {
  rowData.value = record
  isShowTemperature.value = true
}
const onPrintBlood = async (record) => {
  const { code, msg, data } = await post('/inp/Print/printBloodGlucoseTestSlips', { inplsh: record.inplsh })
  if (code !== 0) return message.error(msg)
  window.openExe?.rpt(data) //调用winform打印
}
const onPrintNursingRecord = async (record) => {
  const { code, msg, data } = await post('/inp/InpNursingRecord/printInpNursingRecord', { inpLsh: record.inplsh })
  if (code !== 0) return message.error(msg)
  window.openExe?.rpt(data) //调用winform打印
}
const onPrintAdv = async (record, type) => {
  const { code, msg, data } = await post('/inp/Print/printTemporaryDoctorOrders', {
    sfcybd: '1',
    inpAdvice: type,
    inplsh: record.inplsh
  })
  if (code !== 0) return message.error(msg)
  window.openExe?.rpt(data) //调用winform打印
}
</script>
